<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="店铺名称" prop="shopName">
          <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店铺名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户来源" prop="customerFrom">
          <el-select v-model="queryParams.customerFrom" placeholder="请选择客户来源" clearable>
            <el-option
              v-for="dict in dict.type.fms_customer_from"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="归属平台" prop="shopPlatform">
          <el-select v-model="queryParams.shopPlatform" placeholder="请选择归属平台" clearable>
            <el-option
              v-for="dict in dict.type.fms_shop_platform"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主体" prop="shopDominant">
          <el-select v-model="queryParams.shopDominant" placeholder="请选择主体" clearable>
            <el-option
              v-for="dict in dict.type.fms_shop_dominant"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品类目" prop="productCategory">
          <el-select v-model="queryParams.productCategory" placeholder="请选择产品类目" clearable>
            <el-option
              v-for="dict in dict.type.fms_product_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-select v-model="queryParams.serviceType" placeholder="请选择服务类型" clearable>
            <el-option
              v-for="dict in dict.type.fms_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="服务时间" prop="serviceLength">
          <el-select v-model="queryParams.serviceLength" placeholder="请选择服务时间" clearable>
            <el-option
              v-for="dict in dict.type.fms_service_length"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合作状态" prop="shopStatus">
          <el-select v-model="queryParams.shopStatus" placeholder="请选择合作状态" clearable>
            <el-option
              v-for="dict in dict.type.fms_shop_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="到期日期">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结算日" prop="shopSettleDay">
          <el-input-number v-model="queryParams.shopSettleDay" :min="1" :max="31" placeholder="请输入" clearable></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['finance:shop:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['finance:shop:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="50" align="center" fixed />
        <el-table-column label="ID" align="center" prop="shopId" width="50" fixed />
        
        <el-table-column label="店铺名称" align="center" prop="shopName" width="100" :show-overflow-tooltip="true" />
        <el-table-column label="客户名称" align="center" prop="customerName" width="100" />
        <el-table-column label="客户来源" align="center" prop="customerFrom" width="100" />
        <el-table-column label="归属平台" align="center" prop="shopPlatform" width="100" />
        <!-- <el-table-column label="主体" align="center" prop="shopDominant" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.fms_shop_dominant" :value="scope.row.shopDominant"/>
          </template>
        </el-table-column> -->
        <el-table-column label="主体" align="center" prop="shopDominant" width="100" />
        <el-table-column label="产品类目" align="center" prop="productCategory" width="100" />
        <el-table-column label="产品名称" align="center" prop="productName" width="100" />
        <el-table-column label="服务类型" align="center" prop="serviceType" width="100" />
        <el-table-column label="服务时间" align="center" prop="serviceLength" width="100" />
        <el-table-column label="合作状态" align="center" prop="shopStatus" width="100" />
        <el-table-column label="服务商过期时间" align="center" prop="serviceExpireDate" width="120" />
        <el-table-column label="店铺结算日" align="center" prop="shopSettleDay" width="100" />
        <el-table-column label="创建者" align="center" prop="createBy" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="100">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['finance:shop:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['finance:shop:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="店铺名称" prop="shopName">
                <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
              </el-form-item>
            </el-col>
            <el-col :span="24"></el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerName">
                <el-autocomplete
                  class="inline-input"
                  v-model="form.customerName"
                  :fetch-suggestions="querySearch"
                  placeholder="请输入客户名称"
                  @select="handleSelectCustomer"
                ></el-autocomplete>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户来源">
                <el-select v-model="form.customerFrom" placeholder="请选择客户来源">
                  <el-option
                    v-for="dict in dict.type.fms_customer_from"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="归属平台">
                <el-select v-model="form.shopPlatform" placeholder="请选择归属平台">
                  <el-option
                    v-for="dict in dict.type.fms_shop_platform"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="主体">
                <el-radio-group v-model="form.shopDominant">
                  <el-radio
                    v-for="dict in dict.type.fms_shop_dominant"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品类目">
                <el-select v-model="form.productCategory" placeholder="请选择产品类目">
                  <el-option
                    v-for="dict in dict.type.fms_product_category"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品名称">
                <el-input v-model="form.productName" placeholder="请输入产品名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务类型">
                <el-select v-model="form.serviceType" placeholder="请选择服务类型">
                  <el-option
                    v-for="dict in dict.type.fms_service_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务时间">
                <el-select v-model="form.serviceLength" placeholder="请选择服务时间">
                  <el-option
                    v-for="dict in dict.type.fms_service_length"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合作状态">
                <el-radio-group v-model="form.shopStatus">
                  <el-radio
                    v-for="dict in dict.type.fms_shop_status"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="服务商到期时间">
                <el-date-picker
                  v-model="form.serviceExpireDate"
                  type="date"
                  placeholder="选择日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-col>
            
            <el-col :span="12">
              <el-form-item label="店铺结算日">
                <el-input-number v-model="form.shopSettleDay" :min="1" :max="31" placeholder="请输入"></el-input-number>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listShop, getShop, delShop, addShop, updateShop } from "@/api/finance/shop";
  import { listCustomer } from "@/api/finance/customer";
  
  export default {
    name: "Shop",
    dicts: ['fms_shop_platform', 'fms_shop_dominant', 'fms_product_category', 'fms_service_type', 'fms_service_length', 
    'fms_shop_status', 'fms_customer_from', 'fms_yes_no'],
    data() {
      return {
        //----------
        dateRange: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 公告表格数据
        shopList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          shopName: [
            { required: true, message: "店铺名称不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.loadCustomerList();
    },
    methods: {
      //------------------
      loadCustomerList() {
        listCustomer({}).then(response => {
          let arr = []
          for(let i in response.rows){
            arr.push({"value": response.rows[i].customerName, "id": response.rows[i].customerId})
          }
          this.customerList = arr;
        });
      },
      querySearch(queryString, cb) {
        var restaurants = this.customerList;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelectCustomer(item) {
        this.form.customerId = item.id;
      },
      // ------------------

      /** 查询列表 */
      getList() {
        this.loading = true;
        listShop(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.shopList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          shopId: undefined,
          shopName: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.noticeId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加店铺";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const shopId = row.shopId || this.ids
        getShop(shopId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改店铺";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.shopId != undefined) {
              updateShop(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addShop(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const shopIds = row.shopId || this.ids
        this.$modal.confirm('是否确认删除店铺编号为"' + shopIds + '"的数据项？').then(function() {
          return delShop(shopIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
  </script>
  